﻿using SIAKAD.Helper;
using SIAKADCORE.Controllers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SIAKAD.Dosens
{
    public partial class PengPertanyaan : System.Web.UI.Page
    {
        private DataSet dsPengaturanPertanyaan;
        private DataSet dsTahun;
        private DataSet dsPertanyaan;

        private Dictionary<string, string> isi_pertanyaan;
        private Dictionary<string, string> isi_tahun;

        protected void Page_Load(object sender, EventArgs e)
        {
            CheckSession();
            if (!Page.IsPostBack && !Page.IsCallback)
            {

                this.FillPertanyaan();
                this.IsiDropdownTahun();
                this.InitDetailPertanyaan();
            }
        }

        private void FillPertanyaan()
        {
            isi_pertanyaan = new Dictionary<string, string>();
            var dataset = new DataSet();

            bool isSuccess = PertanyaanController.GetTabel(out dataset);
            if (isSuccess)
            {
                foreach (DataRow row in dataset.Tables[0].Rows)
                {
                    isi_pertanyaan.Add(row[0].ToString(), row[1].ToString());
                }

                foreach (KeyValuePair<string, string> pair in isi_pertanyaan)
                {
                    this.ddlPilihPertanyaan.Items.Add(new ListItem(pair.Value, pair.Key));
                }
            }
        }

        private void IsiDropdownTahun()
        {
            // this.dsTahun = new DataSet();

            isi_tahun = new Dictionary<string, string>();
            var dataset = new DataSet();

            bool isSuccess = TahunController.GetTabel(out dataset);
            if (isSuccess)
            {
                foreach (DataRow row in dataset.Tables[0].Rows)
                {
                    isi_tahun.Add(row[0].ToString(), row[1].ToString());
                }

                foreach (KeyValuePair<string, string> pair in isi_tahun)
                {
                    this.ddlPilihTahun.Items.Add(new ListItem(pair.Value, pair.Key));
                }
            }
        }



        private void InitDetailPertanyaan()
        {
            if (Request.QueryString["thn"] != null  && Request.QueryString["prt"] != null)
            {

                string thn = Request.QueryString["thn"].ToString();
                string prt = Request.QueryString["prt"].ToString();

                bool isSuccess = SIAKADCORE.Controllers.PengaturanPertanyaanController.GetTabelPreview(out this.dsPengaturanPertanyaan, thn,  prt);
                if (isSuccess)
                {
                    this.gvDetailPertanyaan.DataSource = this.dsPengaturanPertanyaan.Tables[0];
                    this.gvDetailPertanyaan.DataBind();
                }
            }
        }

        private void CheckSession()
        {
            if (Session["siakad"] != null && Session["siakad"] is SIAKADCORE.Models.Login)
            {
                if ((Session["siakad"] as SIAKADCORE.Models.Login).UserType != SIAKADCORE.Models.LoginType.Dosen)
                {
                    Response.Redirect(SiteMapHelper.Check((Session["siakad"] as SIAKADCORE.Models.Login)), false);
                    Context.ApplicationInstance.CompleteRequest();
                }
                else
                {
                    this.litName.Text = DosenController.GetName((Session["siakad"] as SIAKADCORE.Models.Login).IdLogin);
                    CheckRole();
                }
            }
            else
            {
                Response.Redirect("/Login.aspx");
            }
        }

        private void CheckRole()
        {
            string username = (Session["siakad"] as SIAKADCORE.Models.Login).Username;
            string idRole = UserController.GetRole(username);

            if (idRole != "RL005")
            {
                Response.Redirect(SiteMapHelper.Check((Session["siakad"] as SIAKADCORE.Models.Login)), false);
                Context.ApplicationInstance.CompleteRequest();
            }
        }

        protected void lnkLogout_Click(object sender, EventArgs e)
        {
            Session.Clear();
            CheckSession();
        }
        protected void SetHiddenField(int index, ref HiddenField hiddenField)
        {
            string code = gvDetailPertanyaan.DataKeys[index].Value.ToString();
            hiddenField.Value = code;
        }

        protected void btPilih_Click(object sender, EventArgs e)
        {
            Response.Redirect("PengPertanyaan.aspx?thn=" + ddlPilihTahun.SelectedValue );
        }

        protected void gvDetailPertanyaan_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gvDetailPertanyaan.PageIndex = e.NewPageIndex;
            this.InitDetailPertanyaan();

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "PagingScript", SiakadScript.PagerStyle(), false);
        }

        protected void gvDetailPertanyaan_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("deleteRecord"))
            {
                int index = Convert.ToInt32(e.CommandArgument);
                this.DeleteCommand(index);
            }
        }

        private void DeleteCommand(int index)
        {
            this.SetHiddenField(index, ref this.hfDeleteID);

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowDeleteModalScript", PengaturanPertanyaanScript.ShowDelete(), false);
        }

        private void ToCurrentPageIndex()
        {
            int index = gvDetailPertanyaan.PageIndex;
            this.InitDetailPertanyaan();
            try
            {
                gvDetailPertanyaan.PageIndex = index;
            }
            catch
            {
                gvDetailPertanyaan.PageIndex = index--;
            }
        }

        protected void btDeletePengaturanPertanyaan_Click(object sender, EventArgs e)
        {

        }
    }
}